from django.contrib.auth.models          import User
from django.contrib.sites.models         import Site
from sitepal.sitebase.models             import Entity,EntityTree
from sitepal.sitebase.tests.models.base  import BaseTest
from sitepal.sitebase.tests.models.trees import TreeTest

class EntityTest(BaseTest):
    def setUp(self):
        self.user = User.objects.create_superuser('admin','admin@localhost.local','admin')
        self.site = Site.objects.get_current()

    def create_object(self,obj,save=True,**kwargs):
        self.assertTrue(isinstance(obj,Entity))
        obj.id      = kwargs.get('id')
        obj.owner   = kwargs.get('owner',self.user)
        obj.creator = kwargs.get('creator',self.user)
        if 'site' in kwargs:
            obj.site = kwargs['site']
        if 'origin' in kwargs:
            obj.origin = kwargs['origin']
        return self.save_object(obj,save=save)

    def verify_object(self,obj,**kwargs):
        self.assertTrue(isinstance(obj,Entity))
        if 'id' in kwargs:
            self.assertTrue(obj.id == kwargs['id'])
        else:
            self.assertTrue(obj.id)
        self.assertTrue(obj.owner   == kwargs.get('owner',self.user))
        self.assertTrue(obj.creator == kwargs.get('creator',self.user))
        self.assertTrue(obj.site    == kwargs.get('site',self.site))
        self.assertTrue(obj.origin  == kwargs.get('origin','[user]'))
        self.assertTrue(obj.date_created)

class EntityTreeTest(EntityTest,TreeTest):
    def create_object(self,obj,save=True,**kwargs):
        self.assertTrue(isinstance(obj,EntityTree))
        obj = EntityTest.create_object(self,obj,save=False,**kwargs)
        obj = TreeTest.create_object(self,obj,save=False,**kwargs)
        return self.save_object(obj,save=save)

    def verify_object(self,obj,**kwargs):
        self.assertTrue(isinstance(obj,EntityTree))
        EntityTest.verify_object(self,obj,**kwargs)
        TreeTest.verify_object(self,obj,**kwargs)

